package Control;

import Database.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.*;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

import Model.*;

public class EditLoyaltyCust extends JFrame implements ActionListener
{
        private JLabel id, customers, name, phone, address, email;
        private JTextField jtID, jtName, jtPhone, jtAddress, jtEmail;
        private JButton search, save, cancel, clear;
        private JPanel leftPanel, rightPanel, logo;
        private TableOperations to;
        private JTable jt;
        private DefaultTableModel jtM;
        private ArrayList users;
        private ArrayList<LoyaltyCustomer> test = new ArrayList<LoyaltyCustomer>();
        private LoyaltyCustomer toBeEdited = null;
        private ImageIcon icon = new ImageIcon("frameIcon.png");

        public EditLoyaltyCust(TableOperations to)
        {
                GridBagLayout layout = new GridBagLayout();
                GridBagConstraints gc = new GridBagConstraints();

                setIconImage(icon.getImage());
                setTitle("MDL - Edit Loyalty Customer");
                setSize(750, 300);
                setLocationRelativeTo(null);
                setResizable(false);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                this.setLayout(new GridLayout(1, 2));
                
                this.to = to;
                Insets insets = new Insets(15, 15, 13, 3);

                leftPanel = new JPanel();
                leftPanel.setLayout(layout);

                id = new JLabel("ID or Name");
                gc.gridx = 0;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(id, gc);

                jtID = new JTextField(10);
                gc.gridx = 0;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, 100, 13, 15);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(jtID, gc);

                search = new JButton("Search");
                search.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(0, 150, 0, 125);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(search, gc);

                clear = new JButton("Clear");
                clear.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(0, 250, 0, 25);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(clear, gc);

                customers = new JLabel("Customers");
                gc.gridx = 0;
                gc.gridy = 2;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 50, 10, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(customers, gc);

                createTable();
                gc.gridx = 0;
                gc.gridy = 3;
                gc.weighty = 1;
                gc.weightx = 3;
                insets = new Insets(10, 25, 20, 30);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(new JScrollPane(jt), gc);

                add(leftPanel);

                insets = new Insets(15, 15, 5, 3);

                rightPanel = new JPanel();
                rightPanel.setLayout(layout);

                name = new JLabel("Name");
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(name, gc);

                jtName = new JTextField();
                gc.gridx = 2;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtName, gc);

                phone = new JLabel("Phone");
                gc.gridx = 0;
                gc.gridy = 3;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(phone, gc);

                jtPhone = new JTextField();
                gc.gridx = 2;
                gc.gridy = 3;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtPhone, gc);

                address = new JLabel("Address");
                gc.gridx = 0;
                gc.gridy = 5;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(address, gc);

                jtAddress = new JTextField();
                gc.gridx = 2;
                gc.gridy = 5;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtAddress, gc);

                email = new JLabel("Email");
                gc.gridx = 0;
                gc.gridy = 7;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(email, gc);

                jtEmail = new JTextField();
                gc.gridx = 2;
                gc.gridy = 7;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtEmail, gc);

                save = new JButton("Save Changes");
                save.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 9;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, 50, 5, 75);
                gc.insets = insets;
                rightPanel.add(save, gc);

                cancel = new JButton("Cancel");
                cancel.addActionListener(this);
                gc.gridx = 2;
                gc.gridy = 9;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -65, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(cancel, gc);

                gc.gridx = 3;
                gc.gridy = 2;
                gc.weighty = 1;
                gc.weightx = 1;
                this.add(rightPanel, gc);

        }

        public void actionPerformed(ActionEvent e)
        {
                if (e.getSource() == cancel)
                        this.dispose();
                else if (e.getSource() == search)
                {
                        if (jtM.getRowCount() > 0)
                        {
                                clear();
                                search();
                        } else
                                search();
                } else if (e.getSource() == clear)
                        clear();
                else if (e.getSource() == save)
                {
                        if (jtName.getText().equals("") || jtAddress.getText().equals("")
                                        || jtPhone.getText().equals("") || jtEmail.getText().equals(""))
                                JOptionPane.showMessageDialog(null, "Check Blank Fields");

                        else
                        {
                                String name = jtName.getText();
                                String address = jtAddress.getText();
                                String phone = jtPhone.getText();
                                String email = jtEmail.getText();
                                
                                boolean valid;
                valid = validateEmailAddress();

                if (!valid) 
                        JOptionPane.showMessageDialog(null,
                                        "Error Invalid E-mail address !");
                else
                {
                                        toBeEdited.setName(name);
                                        toBeEdited.setAddress(address);
                                        toBeEdited.setPhone(phone);
                                        toBeEdited.setEmail(email);
                                        to.updateCustomer(toBeEdited);
                                        clear();
                }
                        }
                }
        }

        // Vaildate Email
        public boolean validateEmailAddress()
        {
                int count = 0;
                int start = 0;

                for (int i = 0; i < jtEmail.getText().length(); i++)
                {
                        i = jtEmail.getText().indexOf("@", start);
                        if (i != -1)
                        {
                                count++;
                                start = i + 1;
                        } else
                        {
                                break;
                        }
                }
                return (count == 1);

        }

        public void createTable()
        {
                jtM = new DefaultTableModel();

                jtM.addColumn("Name");

                fillTable();

                jt = new JTable(jtM);
                jt.addMouseListener(new MouseAdapter()
                {
                        public void mousePressed(MouseEvent e)
                        {
                                int row = jt.getSelectedRow();
                                int custID = test.get(row).getCustID();
                                String name;
                                if(test.get(row).getName().isEmpty())
                                        name = test.get(row).getName();
                                else
                                        name = test.get(row).getName().trim();
                                String address = test.get(row).getAddress();
                                String phone = test.get(row).getPhone();
                                String email = test.get(row).getEmail();
                                java.sql.Date date = test.get(row).getInDate();
                                int points = test.get(row).getPoints();
                                toBeEdited = new LoyaltyCustomer(custID, name, address, phone, email, date, points);
                                System.out.println(custID);
                                jtName.setText(name);
                                jtAddress.setText(address);
                                jtPhone.setText(phone);
                                jtEmail.setText(email);
                        }
                });
        }

        public void clear()
        {
                try
                {
                        jtM.removeRow(0);
                        for (int i = 0; i < jtM.getRowCount(); i++)
                        {
                                jtM.removeRow(i);
                        }

                        test.remove(0);
                        for (int i = 0; i < test.size(); i++)
                        {
                                test.remove(i);
                        }
                } catch (Exception e)
                {
                        System.out.println("No rows to be deleted");
                } finally
                {
                        // jtID.setText("");
                        jtName.setText("");
                        jtAddress.setText("");
                        jtPhone.setText("");
                        jtEmail.setText("");
                }
        }

        public void fillTable()
        {
                if (jtM.getRowCount() >= 1)
                {
                        clear();

                        String[] name = { test.get(test.size() - 1).getName() };
                        jtM.addRow(name);

                } else
                {
                        for (int i = 0; i < test.size(); i++)
                        {
                                String[] name = { test.get(i).getName() };
                                jtM.addRow(name);
                        }
                }
        }

        public void search()
        {
                try
                {
                        test.addAll(to.searchCustomer(Integer.parseInt(jtID.getText())));
                } catch (NumberFormatException nfe)
                {
                        test.addAll(to.searchCustomer(jtID.getText()));
                }
                fillTable();
                if (jtM.getRowCount() <= 0)
                        JOptionPane
                                        .showMessageDialog(null,
                                                        "Customer not found. Hint: Use a CAPITAL LETTER for the 1st Letter");
        }
}